package com.zzy.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zzy.dao.BaseDao;
import com.zzy.model.User;
import com.zzy.service.UserService;
import org.springframework.util.StringUtils;

/**
 * 用户接口的实现
 * @author zzy
 *
 */
@Transactional
@Service()
//springMVC和hibernate结合必须加上不然会报
//No Session found for current thread 异常
public class UserServiceImpl implements UserService {

    @Autowired
    private BaseDao<User> basedao;

    public List<User> getAll(String hql,Object param[]) {
        return basedao.find(hql,param );
    }

    public User getUnique(String hql, Object[] param) {
        return basedao.get(hql,param);
    }

    public User getUserForLogin(String username, String password) {
        return basedao.get("from User where userName=? and password=? ", new Object[]{username,password});
    }

    public User getByID(String  id) {
        return  basedao.get(User.class,id);
    }
    public Integer getTotalNum( String hql ,Object[] param) {
        //从list的size获取总条数的方法不好 数据多了		容易宕机
        long k = basedao.count(hql,param);
        return (int) k;
    }
    public List<User> getPage(String hql, Object[] param, Integer page, Integer rows) {
        return basedao.find(hql, param, page, rows);
    }

    public List<User> getPage(Criteria cr, Integer page, Integer rows) {
        return basedao.find(cr,page,rows);
    }

    public Integer del(String id) {
        int i = 1;
        try {
            if (StringUtils.hasText(id)){
               for(String did : id.split(",")){
                   User o = new User();
                   o=this.getByID(did);
                   basedao.delete(o);
               }
            }
        } catch (Exception e) {
            i = -1;
        }
        return i;
    }
    public Integer saveOrUpdate(User user) {
        int i = 1;
        try {
            basedao.saveOrUpdate(user);
        } catch (Exception e) {
            i =  -1;
        }
        return i;
    }
    public Integer save(User user) {
        int i = 1;
        try {
            basedao.save(user);
        } catch (Exception e) {
            i =  -1;
        }
        return i;
    }
    public Integer update(User user) {
        int i = 1;
        try {
            basedao.update(user);
        } catch (Exception e) {
            i =  -1;
            e.printStackTrace();
        }
        return i;
    }

    public List<User> getList(String hql, Object[] param) {
        return basedao.find(hql, param);
    }

    public List<User> getList(Criteria cr) {
        return basedao.find(cr);
    }

    /**判断用户名是否存在*/
    public boolean isExist(String userName){
        List<User> list = getList(" from User where userName=?" , new Object[]{userName});
        if(list.isEmpty() || list.size()==0){
            return  false;
        }else{
            return  true;
        }

    }


    /**根据role获取用户的ID,组合*/
    public String getUserIdsByRole(String role){
        String ids = "";
        if(StringUtils.hasText(role)){
            List<User> list = getList(" from User where role=?" , new Object[]{role});
            if(!list.isEmpty()){
                for(User o : list){
                    ids = o + "," + ids;
                }
                ids = ids.substring(ids.lastIndexOf(",")+1,ids.length());//去掉最后一个逗号
            }
        }
        return ids;
    }


}
